<template>
  <div class="categories">
    <el-breadcrumb separator="/" style="line-hegiht:45px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div>
      <el-button type="success" plain @click='showAddform'>添加分类</el-button>
    </div>
    <tree-grid
      :treeStructure="true"
      :columns="columns"
      :data-source="dataSource"
      @deleteCate="deleteCategory"
      @editCate="editCategory"
    >
    </tree-grid>
    <!-- 添加商品分类 -->
    <el-dialog title="添加商品分类" :visible.sync="adddialogFormVisible">
      <el-form :model="addform" label-width="80px" :rules="rules" ref="addform">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addform.cat_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父级名称">
          <el-cascader
          v-model="value"
          :options="roleList"
          :props="props"
          clearable></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="adddialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmitForm('addform')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TreeGrid from '@/components/TreeGrid/TreeGrid.vue'
import {getCategories, addCategories} from '@/api/index.js'
export default {
  data () {
    return {
      dataSource: [],
      columns: [{
        text: '分类名称',
        dataIndex: 'cat_name',
        width: ''
      }, {
        text: '是否有效',
        dataIndex: 'cat_deleted',
        width: ''
      }, {
        text: '排序',
        dataIndex: 'cat_level',
        width: ''
      }],
      adddialogFormVisible: false,
      addform: {
        cat_pid: '',
        cat_name: '',
        cat_level: ''
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      roleList: [],
      value: [],
      props: {
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  mounted () {
    this.updatePage()
  },
  methods: {
    // 添加商品分类提交
    addSubmitForm (addform) {
      // 获取选中的项id，利用选中项的长度获取分类的层级
      if (this.value.length === 0) {
        this.addform.cat_pid = 0
        this.addform.cat_level = 0
      } else if (this.value.length === 1) {
        this.addform.cat_pid = this.value[0]
        this.addform.cat_level = 1
      } else if (this.value.length === 2) {
        this.addform.cat_pid = this.value[1]
        this.addform.cat_level = 2
      }
      console.log(this.addform)
      // 开始添加分类操作
      addCategories(this.addform).then(res => {
        console.log(res)
        if (res.meta.status === 201) {
          this.$message.success(res.meta.msg)
        } else {
          this.$message.error(res.meta.msg)
        }
      })
      // 关闭对话框
      this.adddialogFormVisible = false
      // 重置并级选择器
      this.value = []
      // 重置表单
      this.$refs[addform].resetFields()
      // 刷新页面
      this.updatePage()
    },
    // 打开添加表单
    showAddform () {
      this.adddialogFormVisible = true
      // 父级名称选择器
      getCategories({'type': 2}).then(res => {
        this.roleList = res.data
      })
    },
    // 刷新页面
    updatePage () {
      getCategories({'type': 2}).then(res => {
        this.dataSource = res.data
      })
    },
    deleteCategory (cid) {
      console.log(cid)
    },
    editCategory (cid) {
      console.log(cid)
    }
  },
  components: {
    'tree-grid': TreeGrid
  }
}
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  background-color: #F5F5F5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
</style>
